package com.fita.scheduler.handler.impl;

import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.demo2do.core.entity.exception.BusinessException;
import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.http.annotation.Rao;
import com.demo2do.core.utils.JsonUtils;
import com.fita.entity.dto.ehi.response.LocationDetailResponse;
import com.fita.entity.dto.ehi.response.VehicleCore;
import com.fita.entity.dto.ehi.response.VehicleReservationCancleResponse;
import com.fita.entity.dto.ehi.response.VehicleReservationResponse;
import com.fita.entity.dto.ehi.response.VehicleSearchResponse;
import com.fita.scheduler.handler.EhiHandler;
import com.fita.scheduler.rao.EhiRao;

/**
 * 
 * @author mohanlan
 *
 */
@Component("ehiHandler")
public class EhiHandlerImpl implements EhiHandler {

    private static final Logger logger = LoggerFactory.getLogger(EhiHandler.class);

    @Value("#{ctx['api.host']}")
    private String apiHost;
    
    @Value("#{ctx['api.mock']}")
    private boolean apiMock;

    @Rao
    private EhiRao ehiRao;

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.wechat.handler.EhiHandler#searchAvaliableCars(java.util.Map)
     */
    public List<VehicleCore> searchAvaliableCars(Map<String, Object> params) {

//        logger.info("start call rao with params [{}]", JsonUtils.toJson(params));
        HttpResult result = ehiRao.searchVehicles(apiHost + "/api/vehicles/search-available?mock=false", params);
        
//        logger.info("call rao success with result [{}]", JsonUtils.toJson(result));
        if (!result.isValid()) {
            return null;
        }

        String responseString = JsonUtils.toJson(result.get("response"));
        logger.info("searchAvaliableCars response[{}]", responseString);
        VehicleSearchResponse vehicleSearchResponse = JsonUtils.parse(responseString, VehicleSearchResponse.class);
        return vehicleSearchResponse.getVehicleCores();
    }

    /*
     * (non-Javadoc)
     * 
     * @see com.fita.wechat.handler.EhiHandler#loadLocationDetail(java.util.Map)
     */
    public LocationDetailResponse loadLocationDetail(Map<String, Object> params) {

        logger.info("start call rao with params [{}]", JsonUtils.toJson(params));
        HttpResult result =
                ehiRao.locationDetail(apiHost + "/api/vehicles/location-detail?mock=" + apiMock, params);
        
        logger.info("call rao success with result [{}]", JsonUtils.toJson(result));
        if (!result.isValid()) {
            return null;
        }
        
        String responseString = JsonUtils.toJson(result.get("response"));
        LocationDetailResponse locationDetailResponse = JsonUtils.parse(responseString, LocationDetailResponse.class);
        
        return locationDetailResponse;
    }



    
    
    //下单

    
    /*
     * (non-Javadoc)
     * @see com.fita.wechat.handler.EhiHandler#vehicleReservation(java.util.Map)
     */
	public VehicleReservationResponse vehicleReservation(Map<String, Object> params) {
		
		 logger.info("start call rao with params [{}]", JsonUtils.toJson(params));
	        HttpResult result =
	                ehiRao.vehicleReservation(apiHost + "/api/vehicles/reservation-vehicle?mock=" + apiMock, params);
	        
        logger.info("call rao success with result [{}]", JsonUtils.toJson(result));
        
        if (!result.isValid()) {
        	
        	throw BusinessException.newInstance(result.getCode(), result.getMessage());
        }		
        String responseString = JsonUtils.toJson(result.get("response"));
        VehicleReservationResponse vehicleReservationResponse = JsonUtils.parse(responseString, VehicleReservationResponse.class);
        
        return vehicleReservationResponse;
		
	}

	
	//取消订单
	/*
	 * (non-Javadoc)
	 * @see com.fita.wechat.handler.EhiHandler#vehicleReservationCancle(java.util.Map)
	 */
	public VehicleReservationCancleResponse vehicleReservationCancle(Map<String, Object> params) {
		
		logger.info("start call rao with params [{}]", JsonUtils.toJson(params));
		
	    HttpResult result = ehiRao.vehicleReservationCancle(apiHost + "/api/vehicles/cancle-reservation?mock=" + apiMock, params);
	        
        logger.info("call rao success with result [{}]", JsonUtils.toJson(result));
        
        if (!result.isValid()) {
        	
        	throw BusinessException.newInstance(result.getCode(), result.getMessage());
        }		
        String responseString = JsonUtils.toJson(result.get("response"));
        
        VehicleReservationCancleResponse vehicleReservationCancleResponse = JsonUtils.parse(responseString, VehicleReservationCancleResponse.class);
        
        return vehicleReservationCancleResponse;
        
	        
	}
    
    
    
}
